package com.app.rest.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.app.rest.object.User;

public class DBmanager {
	
	
	private static Connection getconnection() throws SQLException {
		Connection conn = null;
		try {
			// http://www.devdaily.com/java/edu/JDBC-SQLProcessor/Simple_JDBC_Example.shtml
			// The newInstance() call is a work around for some
			// broken Java implementations
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		} catch (Exception ex) { // handle the error
			ex.printStackTrace();
		}

		conn = DriverManager.getConnection(
				"jdbc:mysql://localhost/babygo?reconnect=true", "root",
				"haha13");
		return conn;
	}

	public static String getAuthentication(User inputUser) throws SQLException {
		String result = "";
		try {
			Connection dbconnect = getconnection();
			dbconnect.setTransactionIsolation(8);
			java.sql.Statement sqlStmt = dbconnect.createStatement();
			String sqlquery = "select userid,password from account where userid = "+'"'+ inputUser.getUserId()+'"';
			ResultSet rs = sqlStmt.executeQuery(sqlquery);
			
			
			// write in data
			while (rs.next()) {
				result += rs.getString("userid");
				//result += rs.getString("password");
			}
			dbconnect.close();
		} catch (SQLException SqlErr) {
			
		}
		
		
		return result;
	}
	
	public static void main(String[] args) throws SQLException{
		User aa = new User("Tony", "test123");
		String bb = DBmanager.getAuthentication(aa);
		System.out.print(bb);
	}
}
